package cn.hls.web.controller.biz;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.hls.entity.GPSWarn;
import cn.hls.entity.GPSWarnHandle;
import cn.hls.entity.vo.Msg;
import cn.hls.entity.vo.PageInfo;
import cn.hls.service.GPSWarnHandleService;
import cn.hls.service.GPSWarnService;
import cn.hls.until.GenericsUtils;
import cn.hls.web.controller.base.BaseController;

/**
 * @fileName: GPSController.java
 * @package: cn.hls.web.controller.biz 　*@Description: GPS控制器 　*@author:
 *           yinghui.zhang 　*@email: uuhui@163.com
 * @date: 2016年11月20日
 * 
 */
@Controller
@RequestMapping("/gps")
public class GPSController extends BaseController<Object> {

	@Resource(name = "gpsWarnService")
	private GPSWarnService service;
	@Resource(name = "gpsWarnHandleService")
	private GPSWarnHandleService gpsWarnHandleService;

	@RequestMapping(value = "/warnInfo")
	public ModelAndView warnInfo(Long id, boolean show, boolean print)
			throws Exception {
		GPSWarn entity = null;
		ModelAndView modelAndView = new ModelAndView();
		if (id != null) {
			entity = service.find(id);
		}
		if(show){
			modelAndView.setViewName("gps/warn_show");
		}else{
			modelAndView.setViewName("gps/warn_info");
		}
		modelAndView.addObject("entity", entity);
		return modelAndView;
	}

	/**
	 * gps预警处理
	 * 
	 * @param id
	 *            gps预警处理编号
	 * @param warnId
	 *            gps预警编号
	 * @param show
	 *            是否显示
	 * @return modelAndView
	 * @throws Exception
	 */
	@RequestMapping("/warnHandleInfo")
	public ModelAndView warnHandleInfo(Long id, Long warnId, boolean show)
			throws Exception {
		GPSWarnHandle entity = null;
		if (id == null) {
			entity = new GPSWarnHandle();
			entity.setGpsWarn(new GPSWarn(warnId));
		} else {
			List<GPSWarnHandle>list=gpsWarnHandleService.findByWarnId(id);
			if(list!=null && !list.isEmpty()){
				entity=list.get(0);
			}
		}
		ModelAndView modelAndView = new ModelAndView();
		if (show) {
			modelAndView.setViewName("gps/warnHandle_show");
		} else {
			modelAndView.setViewName("gps/warnHandle_info");
		}
		modelAndView.addObject("entity", entity);
		return modelAndView;
	}

	/**
	 * 新增gps预警处理
	 * 
	 * @param entity
	 *            gps预警处理对象
	 * @param request
	 *            http Request请求
	 * @return msg
	 * @throws Exception
	 */
	@RequestMapping("/addWarnHandle")
	@ResponseBody
	public Msg addWarnHandle(GPSWarnHandle entity, HttpServletRequest request)
			throws Exception {
		String handleTime = request.getParameter("handleTime");// 装货时间
		entity.setHandleTime(GenericsUtils.StringToDate(handleTime,
				"yyyy-MM-dd HH:mm:ss"));
		if (gpsWarnHandleService.add(entity)) {
			msg.setInfo("操作成功");
			msg.setStatus(200);
		} else {
			msg.setInfo("操作失败");
			msg.setStatus(201);
		}
		return msg;
	}

	@RequestMapping({ "/warnView" })
	public ModelAndView warnView(HttpServletRequest request) throws Exception {
		return new ModelAndView("/gps/warn_list");
	}

	/**
	 * 删除预警信息
	 * @param ids 
	 * @return msg
	 * @throws Exception
	 */
	@RequestMapping("/warnDel")
	@ResponseBody
	public Msg warnDel(String ids) throws Exception {
		try {
			String[] tempIds = ids.split("\\,");
			service.deltedByIds(tempIds);
			msg.setInfo("操作成功");
			msg.setStatus(200);
		} catch (Exception e) {
			msg.setInfo("操作失败");
			msg.setStatus(201);
		}
		return msg;
	}

	@RequestMapping({ "/warnList" })
	@ResponseBody
	public PageInfo<GPSWarn> warnList(PageInfo<GPSWarn> page, GPSWarn entity,
			HttpServletRequest request) throws Exception {
		if (StringUtils.hasText(request.getParameter("startTime"))) {
			entity.setStartTime(GenericsUtils.StringToDate(
					request.getParameter("startTime"), "yyyy-MM-dd HH:mm:ss"));
		}
		if (StringUtils.hasText(request.getParameter("enTime"))) {
			entity.setStartTime(GenericsUtils.StringToDate(
					request.getParameter("endTime"), "yyyy-MM-dd HH:mm:ss"));
		}
		return this.service.findByPage(page, entity);
	}
}
